﻿using ARPaintServer.Model.Artwork;
using ARPaintServer.Model.OTD;
using Microsoft.AspNetCore.Mvc;


namespace ARPaintServer.Controllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class ArtworkController : ControllerBase
	{
		private readonly IArtworkService _artworkService;

		public ArtworkController(IArtworkService artworkService)
		{
			_artworkService = artworkService;
		}

		[HttpGet("info/{amount}")]
		public async Task<ActionResult<List<Artwork>>> GetArtworkInfo(int amount=100)
		{
			List<ArtworkInfo> artworkInfos =await _artworkService.GetArtworkInfoAsync(amount);
			return Ok(artworkInfos);
		}

		[HttpGet("info/author/{author}")]
		public async Task<ActionResult<List<ArtworkInfo>>> GetArtworkInfoFromAuthor(string author)
		{
			List<ArtworkInfo> artworkInfos = await _artworkService.GetArtworkInfoFromAuthorAsync(author,100);
			return Ok(artworkInfos);
			
		}

		[HttpGet("{id}")]
		public async Task<ActionResult<Artwork>> GetArtworkFromId(string id)
		{
			Artwork artwork=await _artworkService.GetArtworkFromIDAsync(id);
			return Ok(artwork);
		}

		[HttpPost("post")]
		[RequestSizeLimit(52428800)]
		public async Task<ActionResult<int>> SaveArtwork(Artwork artwork)
		{
			try
			{
				await _artworkService.SaveArtworkAsync(artwork);
				return Ok(1);
			}
			catch(Exception ex)
			{
				Console.WriteLine($"保存时发生了错误:{ex.Message}");
				return Forbid();
			}			
		}
	}
}
